﻿using EASEater.Models;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace EASEater
{
    /// <summary>
    /// Interaction logic for DataDicGrid.xaml
    /// </summary>
    public partial class DataDicGrid : UserControl
    {
        public static readonly DependencyProperty TableSourceProperty;

        public static readonly DependencyProperty FieldSourceProperty;

        public static readonly DependencyProperty TableKeyProperty;

        public static readonly DependencyProperty FieldKeyProperty;

        static DataDicGrid()
        {
            TableSourceProperty = DependencyProperty.RegisterAttached("TableSource", typeof(List<Map>), typeof(DataDicGrid));
            FieldSourceProperty = DependencyProperty.RegisterAttached("FieldSource", typeof(List<Map>), typeof(DataDicGrid));
            TableKeyProperty = DependencyProperty.RegisterAttached("TableKey", typeof(string), typeof(DataDicGrid), new PropertyMetadata(TableKeyChanged));
            FieldKeyProperty = DependencyProperty.RegisterAttached("FieldKey", typeof(string), typeof(DataDicGrid), new PropertyMetadata(FieldKeyChanged));
        }

        static void TableKeyChanged(DependencyObject dobj, DependencyPropertyChangedEventArgs e)
        {
            var grid = dobj as DataDicGrid;
            grid.FindTable(e.NewValue.ToString());
        }

        static void FieldKeyChanged(DependencyObject dobj, DependencyPropertyChangedEventArgs e)
        {
            var grid = dobj as DataDicGrid;
            grid.FindField(e.NewValue.ToString());
        }

        public List<Map> TableSource
        {
            get { return (List<Map>)GetValue(TableSourceProperty); }
            set { SetValue(TableSourceProperty, value); }
        }

        public List<Map> FieldSource
        {
            get { return (List<Map>)GetValue(FieldSourceProperty); }
            set { SetValue(FieldSourceProperty, value); }
        }

        public string TableKey
        {
            get { return (string)GetValue(TableKeyProperty); }
            set { SetValue(TableKeyProperty, value); }
        }

        public string FieldKey
        {
            get { return (string)GetValue(FieldKeyProperty); }
            set { SetValue(FieldKeyProperty, value); }
        }

        public DataDicGrid()
        {
            InitializeComponent();
        }

        void FindTable(string key) 
        {
            if (TableSource == null) return;

            Like like = new Like(key);
            grid_tables.ItemsSource =
                string.IsNullOrWhiteSpace(key)
                ? TableSource
                : TableSource.FindAll(like.Search);
        }

        void FindField(string key)
        {
            if (FieldSource == null) return;

            Like like = new Like(key);
            like.Parent = grid_tables.SelectedItem as Map;
            grid_cols.ItemsSource = FieldSource.FindAll(like.Search);
        }

        private void grid_tables_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            FindField(FieldKey);
        }
    }
}
